package com.yd.controller.system;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yd.controller.yd.YD_Sys;
import com.yd.pojo.OrdersBeizhu;
import com.yd.service.OrdersBeizhuService;
import com.yd.tool.UtsReg;

@Controller
public class OrdersBeizhuSys {
	@Resource
	private OrdersBeizhuService ordersBeizhuService;
	
	@RequestMapping(value = {"/sys/ordersBeizhu/list"})
	@ResponseBody
	public String app_orders_list(HttpServletRequest request) throws MalformedURLException, IOException {
		JSONObject jsonObject = new JSONObject();
		
		String token = "";
		String ddid="";
		String page="";	
		String size="";
		
		if(UtsReg.getStringKB(request.getParameter("token"))) {token = request.getParameter("token").trim();} else {jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if(UtsReg.getStringKB(request.getParameter("ddid"))){ddid=request.getParameter("ddid").trim();}
		if(UtsReg.getIntXl(request.getParameter("page"))){page=request.getParameter("page").trim();}else{page = "1";}
		if(UtsReg.getIntXl(request.getParameter("size"))){size=request.getParameter("size").trim();}else{size = "10";}
		
		//验证token
		JSONObject yd_users = YD_Sys.getshidByToken(token);
		if (yd_users.getString("error").equals("1")) {jsonObject.put("msg", yd_users.getString("msg"));jsonObject.put("error", "1");return jsonObject.toString();}
		JSONObject users_token = JSONObject.parseObject(yd_users.getString("data"));
		
		PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(size));
		List<OrdersBeizhu> list = null;
		EntityWrapper<OrdersBeizhu> ew=new EntityWrapper<OrdersBeizhu>();
		if(ddid.length()>0){
			ew.eq("ddid", ddid);
		}
		list=ordersBeizhuService.selectList(ew);
		PageInfo<OrdersBeizhu> pages = new PageInfo<OrdersBeizhu>(list);
		
		jsonObject.put("data", pages);
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		return jsonObject.toString();
	}
	
	@RequestMapping(value = {"/sys/ordersBeizhu/add"})
	@ResponseBody
	public String app_orders_add(HttpServletRequest request) throws MalformedURLException, IOException {
		JSONObject jsonObject = new JSONObject();
		
		String token = "";
		String tid = "";
		String doc = "";
		String ddid="";
		String pid="";
		String caozuoyuanname="";
		String caozuoyuanimg="";
		String chaozuoyuanid ="";
		
		if(UtsReg.getStringKB(request.getParameter("token"))) {token = request.getParameter("token").trim();} else {jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		if(UtsReg.getStringKB(request.getParameter("tid"))){tid=request.getParameter("tid").trim();}
		if(UtsReg.getStringKB(request.getParameter("doc"))){doc=request.getParameter("doc").trim();}
		if(UtsReg.getStringKB(request.getParameter("ddid"))){ddid=request.getParameter("ddid").trim();}
		if(UtsReg.getStringKB(request.getParameter("pid"))){pid=request.getParameter("pid").trim();}
		if(UtsReg.getStringKB(request.getParameter("caozuoyuanname"))){caozuoyuanname=request.getParameter("caozuoyuanname").trim();}
		if(UtsReg.getStringKB(request.getParameter("caozuoyuanimg"))){caozuoyuanimg=request.getParameter("caozuoyuanimg").trim();}
		if(UtsReg.getStringKB(request.getParameter("chaozuoyuanid"))){chaozuoyuanid=request.getParameter("chaozuoyuanid").trim();}
		
		//验证token
		JSONObject yd_users = YD_Sys.getshidByToken(token);
		if (yd_users.getString("error").equals("1")) {jsonObject.put("msg", yd_users.getString("msg"));jsonObject.put("error", "1");return jsonObject.toString();}
		JSONObject users_token = JSONObject.parseObject(yd_users.getString("data"));
		
		OrdersBeizhu beizhu = new OrdersBeizhu();
		beizhu.setTid(tid);
		beizhu.setDoc(doc);
		beizhu.setPid(pid);
		beizhu.setDdid(ddid);
		beizhu.setCaozuoyuanname(caozuoyuanname);
		beizhu.setCaozuoyuanimg(caozuoyuanimg);
		beizhu.setCaozuoyuanid(chaozuoyuanid);
		
		if(!ordersBeizhuService.insert(beizhu)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		return jsonObject.toString();
	}
	
	
	@RequestMapping(value = {"/sys/ordersBeizhu/up"})
	@ResponseBody
	public String app_orders_up(HttpServletRequest request) throws MalformedURLException, IOException {
		JSONObject jsonObject = new JSONObject();
		
		String token = "";
		String id="";
		String tid = "";
		String doc = "";
		String ddid="";
		String pid="";
		String caozuoyuanname="";
		String caozuoyuanimg="";
		String chaozuoyuanid ="";
		/*String page="";
		String size="";*/
		
		if(UtsReg.getStringKB(request.getParameter("token"))) {token = request.getParameter("token").trim();} else {jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		if(UtsReg.getStringKB(request.getParameter("tid"))){tid=request.getParameter("tid").trim();}
		if(UtsReg.getStringKB(request.getParameter("doc"))){doc=request.getParameter("doc").trim();}
		if(UtsReg.getStringKB(request.getParameter("ddid"))){ddid=request.getParameter("ddid").trim();}
		if(UtsReg.getStringKB(request.getParameter("pid"))){pid=request.getParameter("pid").trim();}
		if(UtsReg.getStringKB(request.getParameter("caozuoyuanname"))){caozuoyuanname=request.getParameter("caozuoyuanname").trim();}
		if(UtsReg.getStringKB(request.getParameter("caozuoyuanimg"))){caozuoyuanimg=request.getParameter("caozuoyuanimg").trim();}
		if(UtsReg.getStringKB(request.getParameter("chaozuoyuanid"))){chaozuoyuanid=request.getParameter("chaozuoyuanid").trim();}
		if(UtsReg.getIntXl(request.getParameter("id"))){
			id=request.getParameter("id").trim();
		}else{
			jsonObject.put("error", "1");jsonObject.put("msg", "编号格式错误");return jsonObject.toString();
		}
		/*if(UtsReg.getIntXl(request.getParameter("page"))){page=request.getParameter("page").trim();}else{page = "1";}
		if(UtsReg.getIntXl(request.getParameter("size"))){size=request.getParameter("size").trim();}else{size = "10";}
		*/
		//验证token
		JSONObject yd_users = YD_Sys.getshidByToken(token);
		if (yd_users.getString("error").equals("1")) {jsonObject.put("msg", yd_users.getString("msg"));jsonObject.put("error", "1");return jsonObject.toString();}
		JSONObject users_token = JSONObject.parseObject(yd_users.getString("data"));
		
		OrdersBeizhu beizhu = new OrdersBeizhu();
		beizhu.setId(Integer.valueOf(id));
		beizhu.setTid(tid);
		beizhu.setDoc(doc);
		beizhu.setPid(pid);
		beizhu.setDdid(ddid);
		beizhu.setCaozuoyuanname(caozuoyuanname);
		beizhu.setCaozuoyuanimg(caozuoyuanimg);
		beizhu.setCaozuoyuanid(chaozuoyuanid);
		
		if(!ordersBeizhuService.insert(beizhu)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		return jsonObject.toString();
	}
}
